layui.use(['jquery', 'common', 'table', 'util', 'form', 'layer'], function() {
	var $ = layui.jquery;
	var common = layui.common;
	var table = layui.table;
	var util = layui.util;
	var form = layui.form;
	var layer = layui.layer;

	table.render({
		elem: '#table',
		url: 'tableData',
		page: true,
		toolbar: '#tableToolbar',
		defaultToolbar: [],
		skin: 'line',
		request: {
			pageName: 'current',
			limitName: 'size'
		},
		cols: [[
			{field: 'avatar', title: '头像', templet: function(d) {
				if (d.avatar) {
					return '<img class="layui-circle table-image" src="' + common.getImageUrl(d.avatar, 50) + '" layer-src="' + common.getImageUrl(d.avatar) + '" alt="' + d.name + '">';
				} else {
					return '';
				}
			}},
			{field: 'username', title: '用户名'},
			{field: 'name', title: '昵称'},
			{field: 'sex', title: '性别'},
			{field: 'status', title: '状态'},
			{field: 'roles', title: '角色'},
			{field: 'addtime', title: '添加时间', minWidth: 168, templet: function(d) {
				return util.toDateString(d.addtime);
			}},
			{field: 'lastLoginTime', title: '最后登录时间', minWidth: 168, templet: function(d) {
				return d.lastLoginTime ? util.toDateString(d.lastLoginTime) : '';
			}},
			{title: '操作', minWidth: 84, toolbar: '#rowToolbar'}
		]],
		done: function(res, curr, count) {
			common.tableImagePhotos();
		}
	});
	table.on('toolbar', function(obj) {
		switch (obj.event) {
		case 'add':
			common.toUrl('form');
			break;
		default:
			break;
		}
	});
	table.on('tool', function(obj) {
		var data = obj.data;

		switch (obj.event) {
		case 'edit':
			common.toUrl('form?id=' + data.id);
			break;
		case 'resetPassword':
			layer.confirm('确定要重置密码吗？', function(index) {
				$.getJSON('resetPassword', {id: data.id}, function(data) {
					layer.msg(data.msg);
				});
				layer.close(index);
			});
			break;
		default:
			break;
		}
	});
	form.on('submit(search)', function(data) {
		table.reload('table', {
			where: data.field,
			page: {
				curr: 1
			}
		});
	});
	$('[lay-filter="search"]').trigger('click');
});